package org.gp.Interfaz;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JDialog;
import org.gp.tringes.GestorIGDatosDueno;
import org.gp.tringes.GestorIGInicio;
import org.gp.tringes.GestorIGRegistroDueno;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

public class IGDatosDueno extends JDialog implements Observer {
	
	private String numFederado;
	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblNumFed;
	private JLabel lblNumFed_1;
	private JLabel lblInvisible;
	private JLabel lblNombre;
	private JLabel lblNombre_1;
	private JButton btnModifNom;
	private JLabel lblApel;
	private JLabel lblApel_1;
	private JButton btnModifApel;
	private JLabel lblContrasea;
	private JLabel lblContrasea_1;
	private JButton btnModifCont;
	private JButton btnDesactivar;
	private JButton btnSalir;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGDatosDueno dialog = new IGDatosDueno("");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGDatosDueno(String pNumFederado) {
		numFederado = pNumFederado;
		GestorIGDatosDueno.getGestorIGDatosDueno().obtenerDatosDueno(numFederado);
		GestorIGRegistroDueno.getGestorIGRegistroDueno().addObserver(this);
		
		setBounds(100, 100, 500, 240);
		//ID-122
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(122));
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
		}
		return panel;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(new GridLayout(0, 3, 0, 10));
			panel_1.add(getLblNumFed());
			panel_1.add(getLblNumFed_1());
			panel_1.add(getLblInvisible());
			panel_1.add(getLblNombre());
			panel_1.add(getLblNombre_1());
			panel_1.add(getBtnModifNom());
			panel_1.add(getLblApel());
			panel_1.add(getLblApel_1());
			panel_1.add(getBtnModifApel());
			panel_1.add(getLblContrasea());
			panel_1.add(getLblContrasea_1());
			panel_1.add(getBtnModifCont());
		}
		return panel_1;
	}
	private JLabel getLblNumFed() {
		if (lblNumFed == null) {
			//ID-123
			lblNumFed = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(123));
			lblNumFed.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNumFed;
	}
	private JLabel getLblNumFed_1() {
		if (lblNumFed_1 == null) {
			lblNumFed_1 = new JLabel(GestorIGDatosDueno.getGestorIGDatosDueno().getNumFederado());
			lblNumFed_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNumFed_1;
	}
	private JLabel getLblInvisible() {
		if (lblInvisible == null) {
			lblInvisible = new JLabel("");
		}
		return lblInvisible;
	}
	private JLabel getLblNombre() {
		if (lblNombre == null) {
			//ID-110
			lblNombre = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(110));
			lblNombre.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNombre;
	}
	private JLabel getLblNombre_1() {
		if (lblNombre_1 == null) {
			lblNombre_1 = new JLabel(GestorIGDatosDueno.getGestorIGDatosDueno().getNombre());
			lblNombre_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNombre_1;
	}
	private JButton getBtnModifNom() {
		if (btnModifNom == null) {
			//ID-111
			btnModifNom = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(111));
			btnModifNom.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					setVisible(false);
					JDialog cambiarNombre = new IGModifDatosDueno(lblNombre.getText(),lblNumFed_1.getText());
					cambiarNombre.setResizable(false);
					cambiarNombre.setModal(true);
					cambiarNombre.setLocationRelativeTo(null);
					cambiarNombre.setVisible(true);
					setVisible(true);
				}
			});
		}
		return btnModifNom;
	}
	private JLabel getLblApel() {
		if (lblApel == null) {
			//ID-124
			lblApel = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(124));
			lblApel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblApel;
	}
	private JLabel getLblApel_1() {
		if (lblApel_1 == null) {
			lblApel_1 = new JLabel(GestorIGDatosDueno.getGestorIGDatosDueno().getApel());
			lblApel_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblApel_1;
	}
	private JButton getBtnModifApel() {
		if (btnModifApel == null) {
			//ID-125
			btnModifApel = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(125));
			btnModifApel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					setVisible(false);
					JDialog cambiarApel = new IGModifDatosDueno(lblApel.getText(),lblNumFed_1.getText());
					cambiarApel.setResizable(false);
					cambiarApel.setModal(true);
					cambiarApel.setLocationRelativeTo(null);
					cambiarApel.setVisible(true);
					setVisible(true);
				}
			});
		}
		return btnModifApel;
	}
	private JLabel getLblContrasea() {
		if (lblContrasea == null) {
			//ID-114
			lblContrasea = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(114));
			lblContrasea.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblContrasea;
	}
	private JLabel getLblContrasea_1() {
		if (lblContrasea_1 == null) {
			lblContrasea_1 = new JLabel(GestorIGDatosDueno.getGestorIGDatosDueno().getContra());
			lblContrasea_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblContrasea_1;
	}
	private JButton getBtnModifCont() {
		if (btnModifCont == null) {
			//ID-115
			btnModifCont = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(115));
			btnModifCont.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					setVisible(false);
					JDialog cambiarContra = new IGModifContraDueno(lblNumFed_1.getText());
					cambiarContra.setResizable(false);
					cambiarContra.setModal(true);
					cambiarContra.setLocationRelativeTo(null);
					cambiarContra.setVisible(true);
					setVisible(true);
				}
			});
		}
		return btnModifCont;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.add(getBtnDesactivar());
			panel_2.add(getBtnSalir());
		}
		return panel_2;
	}
	private JButton getBtnDesactivar() {
		if (btnDesactivar == null) {
			//ID-119
			btnDesactivar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(119));
			btnDesactivar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					//ID-120 y 4
					int opcion = JOptionPane.showConfirmDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(120), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.YES_NO_OPTION);
					if(opcion == 0){
						GestorIGDatosDueno.getGestorIGDatosDueno().desactivarCuenta(numFederado);
						//ID-121 y 6
						JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(121), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
						dispose();
					}
				}
			});
		}
		return btnDesactivar;
	}
	private JButton getBtnSalir() {
		if (btnSalir == null) {
			//ID-25
			btnSalir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(25));
			btnSalir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
		}
		return btnSalir;
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		GestorIGDatosDueno.getGestorIGDatosDueno().obtenerDatosDueno(numFederado);
		if((Boolean)arg.equals("Nombre") || (Boolean)arg.equals("Izena")){
			lblNombre_1.setText(GestorIGDatosDueno.getGestorIGDatosDueno().getNombre());
		}else{
			if((Boolean)arg.equals("Apellidos") || (Boolean)arg.equals("Abizenak")){
				lblApel_1.setText(GestorIGDatosDueno.getGestorIGDatosDueno().getApel());
			}else{
				lblContrasea_1.setText(GestorIGDatosDueno.getGestorIGDatosDueno().getContra());
			}
		}
	}
}
